package com.web_bloks.mvc.client;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
public class Observer extends Object implements IObserver {

	private Object context;

	private IMethod notify;

	public Observer() {
		super();
	}
	
	public void init( IMethod notify, Object context ) {
		this.setNotifyContext( context );
		this.setNotifyMethod( notify );		
	}
	/**
	 * Constructor.
	 * 
	 * <P>
	 * The notification method on the interested object should take one
	 * parameter of type <code>INotification</code>
	 * </P>
	 * 
	 * @param notify
	 *            the notification method of the interested object
	 * @param context
	 *            the notification context of the interested object
	 */
	public Observer( IMethod notify, Object context ) {
		super();
		this.setNotifyContext( context );
		this.setNotifyMethod( notify );
	}

	/**
	 * Compare an object to the notification context.
	 * 
	 * @param object
	 *            the object to compare
	 * @return boolean indicating if the object and the notification context are
	 *         the same
	 */
	public boolean compareNotifyContext( Object object ) {
		return this.context == object;
	}

	/**
	 * Notify the interested object.
	 * 
	 * @param notification
	 *            the <code>INotification</code> to pass to the interested
	 *            object's notification method.
	 */
	public void notifyObserver( INotification notification ) {
		this.getNotifyMethod().onNotification( notification );
	}

	/**
	 * Set the notification context.
	 * 
	 * @param notifyContext
	 *            the notification context (this) of the interested object.
	 */
	public void setNotifyContext( Object notifyContext ) {
		this.context = notifyContext;
	}

	/**
	 * Set the notification method.
	 * 
	 * <P>
	 * The notification method should take one parameter of type
	 * <code>INotification</code>.
	 * </P>
	 * 
	 * @param notifyMethod
	 *            the notification (callback) method of the interested object.
	 */
	public void setNotifyMethod( IMethod notifyMethod ) {
		this.notify = notifyMethod;
	}

	/**
	 * Get the notification method.
	 * 
	 * @return the notification (callback) method of the interested object.
	 */
	public IMethod getNotifyMethod( ) {
		return this.notify;
	}

	/**
	 * Get the notification context.
	 * 
	 * @return the notification context (<code>this</code>) of the
	 *         interested object.
	 */
	public Object getNotifyContext( ) {
		return this.context;
	}


}//
